# This file is part of Sonedyan.
#
# Sonedyan is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public
# License as published by the Free Software Foundation;
# either version 3 of the License, or (at your option) any
# later version.
#
# Sonedyan is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
# PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public.
# If not, see <http://www.gnu.org/licenses/>.
#
# Copyright (C) 2009-2012 Jimmy Dubuisson <jimmy.dubuisson@gmail.com>

library(igraph)

source("misc.R")

# mm: membership matrix
print.community.vertices <- function(g, mm)
{
	min <- min(mm)
	max <- max(mm)

	for (i in c(min:max))
	{
		cids <- (which(mm == i) - 1)
		print(paste("Community: ", (i + 1)))
		print(paste("size: ", length(cids)))
		#print(V(g)[V(g)$id %in% cids]$name)
		print(V(g)[cids]$name)
		print("---")
	}
}

###
# clustering FA Core
###

# load FA-Core 
#facore <- read.graph(file = "fa-core-graphml.xml", format = "graphml")
#faeb <- edge.betweenness.community(facore)
#faw <- walktrap.community(facore, steps = 5)
#print.community.vertices(facore, faw$membership)

###
# clustering DD Core
###

# load DD-Core 
#ddcore <- read.graph(file = "dd-core-graphml.xml", format = "graphml")
#ddeb <- edge.betweenness.community(ddcore)
#ddw <- walktrap.community(ddcore, steps = 9)
#print.community.vertices(ddcore, ddw$membership)

###
# clustering FA Core colinks
###

# load FA-Core colinks graph
#facocore <- read.graph(file = "fa-colinks-core-graphml.xml", format = "graphml")
#print.graph.all.stats(facocore, "FA-Core colinks")
#print.graph.global.stats(facocore, "FA-Core colinks")
#facow <- walktrap.community(facocore, steps = 9)
#print.community.vertices(facocore, facow$membership)

###
# clustering k-length cycles induced subgraph 
###

#facycles <- read.graph(file = "fa-subgraph-graphml-4-cycles.xml", format = "graphml")
#faw <- walktrap.community(facycles, steps = 3)
#print.community.vertices(facycles, faw$membership)

###
# clustering k-length cycles reductions
###

rg <- read.graph(file = "reduced-fa-2-cycles-graphml.xml", format = "graphml")

# get main SCC
mainSccVertexIndexes = get.main.cc.vertex.indexes(rg)
rgcore <- subgraph(rg, mainSccVertexIndexes)

rgw <- walktrap.community(rgcore, steps = 2)
print.community.vertices(rgcore, rgw$membership)
